package chapter4_1_cas;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author huayuhao
 * @date 2020/12/16 16:03
 */
public class CASTest {

    AtomicInteger num = new AtomicInteger(0);

    public void inc(){
        for(;;){
            int current = num.get();
            int next = current + 1;
            if(num.compareAndSet(current,next)) {
                return;
            }
        }
    }

    public int value(){
        return num.get();
    }

    public static void main(String[] args) {
        CASTest t = new CASTest();
        for(int i = 0; i < 10; ++i){
            t.inc();
        }
        System.out.println(t.value());
    }
}
